package com.bm.beautifulmouse.effect;

import com.bm.beautifulmouse.model.EffectConfig;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Circle;
import javafx.util.Duration;
import java.util.Random;

public class ParticleEffect implements Effect {
    private final Random random = new Random();

    @Override
    public void generate(Pane container, double x, double y, EffectConfig config) {
        for (int i = 0; i < config.getParticleCount(); i++) {
            Circle particle = new Circle(1.5 * config.getSize());
            // 设置颜色
            particle.setFill(config.getColor());
            
            particle.setCenterX(x);
            particle.setCenterY(y);

            double angle = random.nextDouble() * 360;
            double speed = (random.nextDouble() * 100 + 50) * config.getRange();
            double duration = 1.5 / config.getSpeed();

            Timeline timeline = new Timeline(
                    new KeyFrame(Duration.ZERO,
                            new KeyValue(particle.centerXProperty(), x),
                            new KeyValue(particle.centerYProperty(), y),
                            new KeyValue(particle.opacityProperty(), 1.0),
                            new KeyValue(particle.radiusProperty(), 1.5 * config.getSize())
                    ),
                    new KeyFrame(Duration.seconds(duration),
                            new KeyValue(particle.centerXProperty(), x + Math.cos(Math.toRadians(angle)) * speed),
                            new KeyValue(particle.centerYProperty(), y + Math.sin(Math.toRadians(angle)) * speed),
                            new KeyValue(particle.opacityProperty(), 0.0),
                            new KeyValue(particle.radiusProperty(), 0.1)
                    )
            );

            timeline.setOnFinished(e -> container.getChildren().remove(particle));
            container.getChildren().add(particle);
            timeline.play();
        }
    }
}